package at.fhv.components.closing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.media.jai.KernelJAI;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import at.fhv.framework.customizerTools.MatrixTablePanel;

/**
 * The DilateCustomizer class.
 * 
 * @author Michael Sieber
 */
public class ClosingCustomizer extends JPanel implements Customizer, ActionListener 
{
	private static final long serialVersionUID = -5576304645898860859L;
	
	private PropertyChangeSupport _pcsupport = new PropertyChangeSupport(this);
	
	private JButton _applyButton;
	private MatrixTablePanel _erodeMatrixTabelPanel;
	private MatrixTablePanel _dilateMatrixTabelPanel;
	private ClosingFilter _closingFilter;

	public ClosingCustomizer() 
	{
		initGUI();
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void setObject(Object obj)
	{
		_closingFilter = (ClosingFilter) obj;
		
		KernelJAI erodeKernel = _closingFilter.getErodeKernel();
		KernelJAI dilateKernel = _closingFilter.getDilateKernel();
		
		_erodeMatrixTabelPanel.setTableData(erodeKernel.getKernelData(), erodeKernel.getHeight(), erodeKernel.getWidth());
		_dilateMatrixTabelPanel.setTableData(dilateKernel.getKernelData(), dilateKernel.getHeight(), dilateKernel.getWidth());
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener pcl)
	{
		_pcsupport.addPropertyChangeListener(pcl);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener pcl) 
	{
		_pcsupport.removePropertyChangeListener(pcl);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void actionPerformed(ActionEvent evt) 
	{
		if (evt.getSource().equals(_applyButton)) 
		{
			_closingFilter.setErodeKernel
			(
					new KernelJAI
					(
						_erodeMatrixTabelPanel.getKernelHeight(),
						_erodeMatrixTabelPanel.getKernelWidth(),
						_erodeMatrixTabelPanel.translateKernel()
					)
			);
			
			_closingFilter.setDilateKernel
			(
					new KernelJAI
					(
						_dilateMatrixTabelPanel.getKernelHeight(),
						_dilateMatrixTabelPanel.getKernelWidth(),
						_dilateMatrixTabelPanel.translateKernel()
					)
			);
		}
	}

	/**
	 * Initialize the gui
	 */
	private void initGUI() 
	{
		setLayout(new BorderLayout());
		_erodeMatrixTabelPanel = new MatrixTablePanel();
		_dilateMatrixTabelPanel = new MatrixTablePanel();
				
		//erode matrix panel
		JPanel erodeMatrixPanel = new JPanel(new BorderLayout());
		JPanel erodeLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		erodeLabelPanel.add(new JLabel("erode matrix"));
		erodeMatrixPanel.add(erodeLabelPanel, BorderLayout.NORTH);
		erodeMatrixPanel.add(_erodeMatrixTabelPanel, BorderLayout.CENTER);
		
		//dilate matrix panel
		JPanel dilateMatrixPanel = new JPanel(new BorderLayout());
		JPanel dilateLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		dilateLabelPanel.add(new JLabel("dilate matrix"));
		dilateMatrixPanel.add(dilateLabelPanel, BorderLayout.NORTH);
		dilateMatrixPanel.add(_dilateMatrixTabelPanel, BorderLayout.CENTER);
		
		_applyButton = new JButton("apply");
		_applyButton.addActionListener(this);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(_applyButton);		
		
		add(erodeMatrixPanel, BorderLayout.WEST);
		add(dilateMatrixPanel, BorderLayout.EAST);
		add(buttonPanel, BorderLayout.SOUTH);
	}
}
